package com.dwjf.core.ui;

import android.content.Context;
import android.support.v7.app.AppCompatDialog;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.dwjf.core.R;
import com.dwjf.core.util.DimeUtil;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.Iterator;

public class LatteLoader {

    private static final int LOADER_SIZE_SCALE = 8;
    private static final int LOADER_OFFSET_SCALE = 10;

    private static final ArrayList<AppCompatDialog> loaders = new ArrayList<>();

    private static final String DEFAULT_LOADER = LoaderStyle.BallClipRotatePulseIndicator.name();

    public static void showLoding(Context context, Enum<LoaderStyle> type){
        showLoding(context, type.name());
    }






    public static void showLoding(Context context, String type){
        AppCompatDialog dialog = new AppCompatDialog(context, R.style.dialog);
        AVLoadingIndicatorView view = LoaderCreator.create(type, context);
        dialog.setContentView(view);
        int deviceWidth = DimeUtil.getScreenWidth();
        int deviceHeight = DimeUtil.getScreenHeight();
        Window window = dialog.getWindow();

        if (window != null){
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = deviceWidth/LOADER_SIZE_SCALE;
            lp.height = deviceHeight/LOADER_SIZE_SCALE;
            lp.height = lp.height+deviceHeight/LOADER_OFFSET_SCALE;
            lp.gravity = Gravity.CENTER;

        }
        loaders.add(dialog);
        dialog.show();
    }

    public static void showLoading(Context context){
        showLoding(context, DEFAULT_LOADER);
    }

    public static void stopLoading(){
        Iterator<AppCompatDialog> iterator = loaders.iterator();
        while (iterator.hasNext()){
            AppCompatDialog dialog = iterator.next();
            if (dialog == null){
                iterator.remove();
                continue;
            }
            dialog.cancel();
            dialog.dismiss();
        }
    }
}
